using Infragistics.Win.UltraWinTree;
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace RetailAnalytic.Tools.MGrouping
{
    public class Common
    {
        static Common()
        {
            Common.groupId = 1;
        }

        public static void clearGrouping(GroupingData grpData)
        {
            string text1 = "Delete From " + grpData.groupTable + " Where Id = Id ";
            new SqlCommand(text1, Common.sqlConnection).ExecuteNonQuery();
            text1 = "Update " + grpData.sourceTable + " Set " + grpData.linkColumn + " = NULL Where " + grpData.itemId + " = " + grpData.itemId;
            new SqlCommand(text1, Common.sqlConnection).ExecuteNonQuery();
        }

        public static void createGrpTable(string name)
        {
            string text1 = "CREATE TABLE " + name + "([ParentID] [int], [ID] [int],\t[Name] [varchar] (50))";
            new SqlCommand(text1, Common.sqlConnection).ExecuteNonQuery();
        }

        public static ArrayList dupeCollection(SelectedNodesCollection src)
        {
            ArrayList list1 = new ArrayList();
            foreach (UltraTreeNode node1 in src)
            {
                list1.Add(node1);
            }
            return list1;
        }

        public static bool Initialize()
        {
            Common.sqlConnectionString = Common.sqlConnectionString + ";PWD=sysadm;UID=sysadm";
            try
            {
                Common.sqlConnection = new SqlConnection(Common.sqlConnectionString);
                Common.sqlConnection.Open();
                return true;
            }
            catch 
            {
                RetailAnalytic.Core.Dialog.Show(RetailAnalytic.Core.Dialog_type.Error, /*SR.StringLibrary.GetStrMSG("*/
                    SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Error_NO_CONNECT_OLAP,
                    Environment.StackTrace, ""));
                return false;
            }
        }

        private static void RecursiveSave(UltraTreeNode node, GroupingData grpData)
        {
            string text3;
            if (node.HasNodes)
            {
                NodeEnumerator enumerator1 = node.Nodes.GetEnumerator();
                try
                {
                    while (enumerator1.MoveNext())
                    {
                        Common.RecursiveSave(enumerator1.Current, grpData);
                    }
                }
                finally
                {
                    IDisposable disposable1 = enumerator1 as IDisposable;
                    if (disposable1 != null)
                    {
                        disposable1.Dispose();
                    }
                }
            }
            NodeData data1 = (NodeData) node.DataKey;
            string text1 = node.Key.Substring(1);
            string text2 = node.Parent.Key.Substring(1);
            if (data1.IsItem)
            {
                text3 = "Update " + grpData.sourceTable + " Set " + grpData.linkColumn + " = " + text2 + " Where " + grpData.itemId + " = " + text1;
                new SqlCommand(text3, Common.sqlConnection).ExecuteNonQuery();
            }
            else if (data1.IsGroup)
            {
                text3 = "Insert Into " + grpData.groupTable + " (Id, ParentId, Name)  Values (" + text1 + ", " + text2 + ", '" + node.Text + "')";
                new SqlCommand(text3, Common.sqlConnection).ExecuteNonQuery();
            }
        }

        public static void ResetGroupId(GroupingData grpData)
        {
            string text1 = "select max(ID) from " + grpData.groupTable;
            SqlDataReader reader1 = new SqlCommand(text1, Common.sqlConnection).ExecuteReader();
            reader1.Read();
            if (reader1.IsDBNull(0))
            {
                Common.GroupID = 1;
            }
            else
            {
                Common.GroupID = ((int) reader1.GetValue(0)) + 1;
            }
            reader1.Close();
        }

        public static void SaveTree()
        {
            try
            {
                GroupingData data1 = new GroupingData(Common.sqlConnection, Common.leftNode.Text);
                Common.clearGrouping(data1);
                NodeEnumerator enumerator1 = Common.leftNode.Nodes.GetEnumerator();
                try
                {
                    while (enumerator1.MoveNext())
                    {
                        Common.RecursiveSave(enumerator1.Current, data1);
                    }
                }
                finally
                {
                    IDisposable disposable1 = enumerator1 as IDisposable;
                    if (disposable1 != null)
                    {
                        disposable1.Dispose();
                    }
                }
                try
                {
                    SqlCommand scUG = new SqlCommand("exec UpdateGrouping", Common.sqlConnection);
                    scUG.ExecuteNonQuery();
                }
                catch 
                {
                    
                    
                }
                
            }
            catch 
            {
                
                throw;
            }
        }


        public static int GroupID
        {
            get
            {
                return Common.groupId;
            }
            set
            {
                Common.groupId = value;
            }
        }


        private static int groupId;
        public static UltraTreeNode leftNode;
        public static UltraTreeNode rightNode;
        public static UltraTreeNode rightRest;
        public static SqlConnection sqlConnection;
        public static string sqlConnectionString;
    }
}

